//
//  CustomListCellCell.m
//  DemoMeshtiles
//
//  Created by MAC on 7/19/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CustomListCellCell.h"

@implementation CustomListCellCell
@synthesize egoPhotoImage;
@synthesize egoUserAvatar;
@synthesize nameLabel;
@synthesize postTimeString;
@synthesize tagContentString;
@synthesize captionString;
@synthesize numberClick;
@synthesize numberComment;
@synthesize photoData;
@synthesize photoCurrLength;
@synthesize photoContentLength;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
-(void)dealloc {
    [self.egoPhotoImage release];
    [self.egoUserAvatar release];
    [self.nameLabel release];
    [self.postTimeString release];
    [self.tagContentString release];
    [self.captionString release];
    [super dealloc];
}
-(NSString *)timeToString:(long)time{
    
    NSInteger minute=time/60;
    NSString *result=nil;
    if (minute<60) {
        //Tra loi bao nhieu fut truoc
        result=[[NSString alloc] initWithFormat:@"%d phút trước",minute];
    } else {
        NSInteger hour=minute/60;
        if (hour<25) {
             result=[[NSString alloc] initWithFormat:@"%d giờ trước",hour];
            //Tra lai bao nhieu gio
        } else {
            NSInteger day=hour/24;
            if (day<30) {
               result=[[NSString alloc] initWithFormat:@"%d ngày trước",day]; 
            } else {
                NSInteger month=day/30;
                if (month<12)
                result=[[NSString alloc] initWithFormat:@"%d tháng trước",month]; 
            }
        }
    }
 if (result==nil) {
    result= [[NSString alloc] initWithFormat:@"Cách đây rất lâu"]; 
 }
return result;
}
-(NSString *)tagToTagFormat:(NSString*)tag {
    
    NSMutableString *result=[[NSMutableString alloc] init];
    NSArray *array = [tag componentsSeparatedByString:@","];
        for (int i=0;i<[array count];i++) {
        [result appendFormat:@"#%@ ",[array objectAtIndex:i]];
    }
    
    
    return result;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withData:(NSDictionary*)data
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        //Ve separator Image
        UIImageView  *separatorImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]]; 
        separatorImg.frame=CGRectMake(0, 0, 320, 5);
        [self.contentView addSubview:separatorImg];
        //Ve photo image
       egoPhotoImage=[[EGOCustomImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"224.png"]];  
        
        egoPhotoImage.frame=CGRectMake(10 , 10, 300, 300);
      [self.contentView addSubview:egoPhotoImage]; 
      
        
        
        //Ve avatar
        egoUserAvatar=[[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"224.png"]];                
        egoUserAvatar.frame=CGRectMake(10 , 300+20, 50, 50);
        [self.contentView addSubview:egoUserAvatar]; 
    //   [egoUserAvatar setImageURL:[NSURL URLWithString:avatarURL]];
        //Ve nick_name
        CGRect nameLabelRect = CGRectMake(70, 300+20, 215-72, 15);
        nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect]; 
        
        [nameLabel setBackgroundColor:[UIColor colorWithRed:237*1.0f/255 green:234*1.0f/255 blue:229*1.0f/255 alpha:1]];
        nameLabel.font = [UIFont boldSystemFontOfSize:11]; 
        nameLabel.textColor=[UIColor blueColor];
        [self.contentView addSubview: nameLabel];
        //Ve dong ho thoi gian
        
        UIImageView  *postTimeImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"postTime.png"]]; 
        postTimeImg.frame=CGRectMake(210, 300+20, 10, 10);
        [self.contentView addSubview:postTimeImg];
        
        //Ve label thoi gian post
        CGRect postTimeStringRect = CGRectMake(225, 300+20, 310-225, 10);
        postTimeString = [[UILabel alloc] initWithFrame:postTimeStringRect]; 
        postTimeString.textColor=[UIColor grayColor];
     //   postTimeString.text=[self timeToString:timePostByUser];
        postTimeString.font=[UIFont boldSystemFontOfSize:8];
        [postTimeString setBackgroundColor:[UIColor colorWithRed:237*1.0f/255 green:234*1.0f/255 blue:229*1.0f/255 alpha:1]];
        [self.contentView addSubview:postTimeString];
        
        //Ve caption 
        CGRect captionStringRect = CGRectMake(70, 300+40, 310-70, 15);
        captionString = [[UILabel alloc] initWithFrame:captionStringRect]; 
        captionString.textColor=[UIColor grayColor];
        [captionString setBackgroundColor:[UIColor colorWithRed:237*1.0f/255 green:234*1.0f/255 blue:229*1.0f/255 alpha:1]]; 
        postTimeString.font=[UIFont boldSystemFontOfSize:10];
        captionString.font=[UIFont systemFontOfSize:12];
        [self.contentView addSubview:captionString];
        
        //Ve image tag
        UIImageView  *tagImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tag_bullet_bg.png"]]; 
        tagImg.frame=CGRectMake( 10,375, 13, 13);
        [self.contentView addSubview:tagImg];
        //Ve tag label
        CGRect tagStringRect = CGRectMake(10+15, 375, 20, 13);
        UILabel *tagString = [[UILabel alloc] initWithFrame:tagStringRect]; 
        tagString.text=@"tag";
        tagString.font=[UIFont systemFontOfSize:9];
        tagString.textColor=[UIColor blackColor];
        [tagString setBackgroundColor:[UIColor colorWithRed:237*1.0f/255 green:234*1.0f/255 blue:229*1.0f/255 alpha:1]];
        [self.contentView addSubview:tagString];
        //Ve string tag content
        CGRect tagContentStringRect = CGRectMake(70, 375, 310-70, 13);
        tagContentString = [[UILabel alloc] initWithFrame:tagContentStringRect]; 
        [tagContentString setBackgroundColor:[UIColor colorWithRed:237*1.0f/255 green:234*1.0f/255 blue:229*1.0f/255 alpha:1]];
        tagContentString.font=[UIFont boldSystemFontOfSize:9];
        tagContentString.textColor=[UIColor blueColor];
        [self.contentView addSubview:tagContentString];
        
        //Ve image with
        UIImageView  *withImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"with_bullet_bg.png"]]; 
        withImg.frame=CGRectMake( 10,390, 13, 13);
        [self.contentView addSubview:withImg];
        //Ve with label
        CGRect withStringRect = CGRectMake(10+15, 390, 20, 13);
        UILabel *withString = [[UILabel alloc] initWithFrame:withStringRect]; 
        withString.textColor=[UIColor grayColor];
        withString.text=@"với";
        withString.font=[UIFont systemFontOfSize:9];
        withString.textColor=[UIColor blackColor];
        [withString setBackgroundColor:[UIColor colorWithRed:237*1.0f/255 green:234*1.0f/255 blue:229*1.0f/255 alpha:1]];
        [self.contentView addSubview:withString];
        
        
        //Ve button Type
        UIImage *buttonTypeUIImage=[UIImage imageNamed:@"btnYummy.png"];
        NSInteger buttonTypeWidth=15*buttonTypeUIImage.size.width/buttonTypeUIImage.size.height;
        UIImageView *buttonTypeImg=[[UIImageView alloc] initWithImage:buttonTypeUIImage]; 
        buttonTypeImg.frame=CGRectMake( 10,410, buttonTypeWidth, 15);
        [self.contentView addSubview:buttonTypeImg];
        
        //Ve  NumberAli
        UIImage *numberAliUIImage=[UIImage imageNamed:@"numberAli.png"];
        NSInteger numberAliWidth=15*numberAliUIImage.size.width/numberAliUIImage.size.height;
        UIImageView *numberAliImg=[[UIImageView alloc] initWithImage:numberAliUIImage]; 
        numberAliImg.frame=CGRectMake( 10+buttonTypeWidth+15,410, numberAliWidth, 15);
        [self.contentView addSubview:numberAliImg];
        //Ve numberButtonClick
        CGRect numberBtnClickRect = CGRectMake(10+buttonTypeWidth+15+6, 410+6, numberAliWidth-8, 15-8);
       numberClick = [[UILabel alloc] initWithFrame:numberBtnClickRect]; 
        numberClick .textColor=[UIColor blackColor];
        numberClick.text=@"5";
        numberClick .font=[UIFont boldSystemFontOfSize:9];
        numberClick.textAlignment=UITextAlignmentCenter;
        
        
        [self.contentView addSubview:numberClick ];
        
        //Ve comment button
        UIImage *commentButtonUIImage=[UIImage imageNamed:@"btnComment.png"];
        NSInteger commentButtonWidth=15*commentButtonUIImage.size.width/commentButtonUIImage.size.height;
        UIImageView *commentButtonImg=[[UIImageView alloc] initWithImage:commentButtonUIImage]; 
        commentButtonImg.frame=CGRectMake( 10+buttonTypeWidth+15*2+numberAliWidth,410, commentButtonWidth+3, 15);
        [self.contentView addSubview:commentButtonImg];
        
        //Ve Number Ali2
        UIImageView *numberAli2Img=[[UIImageView alloc] initWithImage:numberAliUIImage]; 
        numberAli2Img.frame=CGRectMake( 10+buttonTypeWidth+15*2+numberAliWidth+commentButtonWidth+10,410, numberAliWidth+1, 15);
        [self.contentView addSubview:numberAli2Img];
        //Ve numberComment
        CGRect numberCommentRect = CGRectMake(10+buttonTypeWidth+15*2+numberAliWidth+commentButtonWidth+10+6, 410+6, numberAliWidth-6, 15-8);
        numberComment = [[UILabel alloc] initWithFrame:numberCommentRect]; 
        numberComment .textColor=[UIColor blackColor];
        numberComment.text=@"5";
       numberComment.font=[UIFont boldSystemFontOfSize:9];
        numberComment.textAlignment=UITextAlignmentCenter;
        
        
        [self.contentView addSubview:numberComment ];

        // Initialization code
    }
    return self;
}

-(void)setCellData:(NSDictionary*) data {
    //Parse JSON data
    //NSLog(@"Inner %@",data);
                     
    //
    NSString *photoURL=[data objectForKey:@"url_photo"];
    long timePostByUser = [[data objectForKey:@"time_post"] longValue];
    
    NSDictionary *userDict=[data objectForKey:@"user"];
    NSString *nickNameStringContent=[userDict objectForKey:@"user_name"];
    NSString *avatarURL=[userDict objectForKey:@"url_image"];
    NSString *tagList=[data objectForKey:@"list_tags"];
    NSString *caption=[data objectForKey:@"caption"];
    int number_click=[[data objectForKey:@"number_click"] intValue];
   int number_comment=[[data objectForKey:@"number_comment"] intValue];
    

    //Set photo image
    [egoPhotoImage setImageURL:[NSURL URLWithString:photoURL]];
	
    
    //Set avatar
    [egoUserAvatar setImageURL:[NSURL URLWithString:avatarURL]];
    //Ve nick_name
    
    
    nameLabel.text = nickNameStringContent;
    
    //set label thoi gian post
    postTimeString.text=[self timeToString:timePostByUser];
    //Set string tag content

    tagContentString.text=[self tagToTagFormat:tagList];
    //
    
    captionString.text=[caption stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //
    numberClick.text=[[NSString alloc] initWithFormat:@"%d",number_click];
    numberComment.text=[[NSString alloc] initWithFormat:@"%d",number_comment];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
@end
